package cn.hamster3.hamsterworld.command;

import cn.hamster3.hamsterworld.core.DataManager;
import cn.hamster3.hamsterworld.core.Message;
import cn.hamster3.hamsterworld.data.WorldData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class RealTimeCommand extends CommandBase {
    public RealTimeCommand(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean isPlayerCommand() {
        return false;
    }

    @Override
    public String getPermission() {
        return "hw.admin";
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length < 2) {
            sender.sendMessage(Message.notInputWorldName.toString());
            return true;
        }
        Player player = (Player) sender;
        WorldData data = dataManager.getWorldData(args[1]);
        if (data == null) {
            player.sendMessage(Message.worldNotExist.toString());
            return true;
        }
        boolean isRealTime = !data.isRealTime();
        if (args.length >= 3) {
            String flag = args[2];
            switch (flag.toLowerCase()) {
                case "true":
                case "on":
                case "open":
                    isRealTime = true;
                    break;
                case "false":
                case "off":
                case "close":
                default:
                    isRealTime = false;
            }
        }
        data.setRealTime(isRealTime);
        if (data.isRealTime()) {
            // 关闭时间推移
            data.getWorld().setGameRuleValue("doDaylightCycle", "false");
        } else {
            // 开启时间推移
            data.getWorld().setGameRuleValue("doDaylightCycle", "true");
        }
        if (isRealTime) {
            sender.sendMessage(Message.realTimeOn.toString());
        } else {
            sender.sendMessage(Message.realTimeOff.toString());
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        ArrayList<String> tab = new ArrayList<>();
        if (!sender.hasPermission("hw.admin")) {
            return tab;
        }
        switch (args.length) {
            case 2:
                for (WorldData data : dataManager.getWorldData()) {
                    if (!data.getName().toLowerCase().startsWith(args[1].toLowerCase())) {
                        continue;
                    }
                    tab.add(data.getName());
                }
                return tab;
            case 3:
                for (String s : new String[]{"on", "off"}) {
                    if (!s.startsWith(args[1].toLowerCase())) {
                        continue;
                    }
                    tab.add(s);
                }
                return tab;
        }
        return tab;
    }
}
